<?php
/**
 * Class that operate on table 'common_footer'. Database Mysql.
 *
 * @author: Ali
 * @date: 2012-02-08 20:52
 */
class CommonFooterMySqlDAO implements CommonFooterDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return CommonFooterMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM common_footer WHERE id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM common_footer';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM common_footer ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param commonFooter primary key
 	 */
	public function delete($id){
		$sql = 'DELETE FROM common_footer WHERE id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($id);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param CommonFooterMySql commonFooter
 	 */
	public function insert($commonFooter){
		$sql = 'INSERT INTO common_footer (text1_en, text2_en, text3_en, text4_en, text1_sv, text2_sv, text3_sv, text4_sv) VALUES (?, ?, ?, ?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($commonFooter->text1En);
		$sqlQuery->set($commonFooter->text2En);
		$sqlQuery->set($commonFooter->text3En);
		$sqlQuery->set($commonFooter->text4En);
		$sqlQuery->set($commonFooter->text1Sv);
		$sqlQuery->set($commonFooter->text2Sv);
		$sqlQuery->set($commonFooter->text3Sv);
		$sqlQuery->set($commonFooter->text4Sv);

		$id = $this->executeInsert($sqlQuery);	
		$commonFooter->id = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param CommonFooterMySql commonFooter
 	 */
	public function update($commonFooter){
		$sql = 'UPDATE common_footer SET text1_en = ?, text2_en = ?, text3_en = ?, text4_en = ?, text1_sv = ?, text2_sv = ?, text3_sv = ?, text4_sv = ? WHERE id = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($commonFooter->text1En);
		$sqlQuery->set($commonFooter->text2En);
		$sqlQuery->set($commonFooter->text3En);
		$sqlQuery->set($commonFooter->text4En);
		$sqlQuery->set($commonFooter->text1Sv);
		$sqlQuery->set($commonFooter->text2Sv);
		$sqlQuery->set($commonFooter->text3Sv);
		$sqlQuery->set($commonFooter->text4Sv);

		$sqlQuery->set($commonFooter->id);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM common_footer';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByText1En($value){
		$sql = 'SELECT * FROM common_footer WHERE text1_en = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByText2En($value){
		$sql = 'SELECT * FROM common_footer WHERE text2_en = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByText3En($value){
		$sql = 'SELECT * FROM common_footer WHERE text3_en = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByText4En($value){
		$sql = 'SELECT * FROM common_footer WHERE text4_en = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByText1Sv($value){
		$sql = 'SELECT * FROM common_footer WHERE text1_sv = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByText2Sv($value){
		$sql = 'SELECT * FROM common_footer WHERE text2_sv = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByText3Sv($value){
		$sql = 'SELECT * FROM common_footer WHERE text3_sv = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByText4Sv($value){
		$sql = 'SELECT * FROM common_footer WHERE text4_sv = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByText1En($value){
		$sql = 'DELETE FROM common_footer WHERE text1_en = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByText2En($value){
		$sql = 'DELETE FROM common_footer WHERE text2_en = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByText3En($value){
		$sql = 'DELETE FROM common_footer WHERE text3_en = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByText4En($value){
		$sql = 'DELETE FROM common_footer WHERE text4_en = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByText1Sv($value){
		$sql = 'DELETE FROM common_footer WHERE text1_sv = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByText2Sv($value){
		$sql = 'DELETE FROM common_footer WHERE text2_sv = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByText3Sv($value){
		$sql = 'DELETE FROM common_footer WHERE text3_sv = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByText4Sv($value){
		$sql = 'DELETE FROM common_footer WHERE text4_sv = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return CommonFooterMySql 
	 */
	protected function readRow($row){
		$commonFooter = new CommonFooter();
		
		$commonFooter->id = $row['id'];
		$commonFooter->text1En = $row['text1_en'];
		$commonFooter->text2En = $row['text2_en'];
		$commonFooter->text3En = $row['text3_en'];
		$commonFooter->text4En = $row['text4_en'];
		$commonFooter->text1Sv = $row['text1_sv'];
		$commonFooter->text2Sv = $row['text2_sv'];
		$commonFooter->text3Sv = $row['text3_sv'];
		$commonFooter->text4Sv = $row['text4_sv'];

		return $commonFooter;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return CommonFooterMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>